cmake_minimum_required(VERSION 2.8)

project(flexible-network)

set(CMAKE_CXX_STANDARD 11)

option(debug "build debug version" OFF)
option(build_static "build static library/application." OFF)

if (UNIX)
    set(CMAKE_C_FLAGS "-fPIC -Wall -pthread")
    set(CMAKE_CXX_FLAGS "-fPIC -Wall -pthread -Wno-unused")
    if (debug)
        set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ggdb2")
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ggdb2")
    else()
        set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O2")
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2")
    endif()
endif()

message(STATUS "${PROJECT_NAME}-CFLAGS: ${CMAKE_C_FLAGS}")
message(STATUS "${PROJECT_NAME}-CXXFLAGS: ${CMAKE_CXX_FLAGS}")

if (CMAKE_SYSTEM_NAME MATCHES "Linux")
    INCLUDE_DIRECTORIES(/usr/local/include)
    LINK_LIBRARIES(/usr/local/lib)
endif()

INCLUDE_DIRECTORIES(.)

if (NOT build_static)
    if(CMAKE_SYSTEM_NAME MATCHES "Windows")
        find_package(Boost COMPONENTS system filesystem regex thread)
        INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
    else()
        find_package(Boost 1.67 COMPONENTS system filesystem regex thread)
    endif()
else()
    set(Boost_LIBRARIES libboost_system.a libboost_filesystem.a libboost_regex.a libboost_thread.a)
endif()


set (TRANSPORT "transport/BaseTransport.cpp" "transport/TcpTransport.cpp")
if (UNIX)
    set (TRANSPORT ${TRANSPORT} "transport/UnixSocketTransport.cpp")
endif()

#aux_source_directory(transport TRANSPORT)
aux_source_directory(protocol PROTOCOL)
aux_source_directory(factory FACTORY)
aux_source_directory(listener LISTENER)

add_executable(${PROJECT_NAME}-client "main.cpp" ${TRANSPORT} ${PROTOCOL} ${FACTORY} ${LISTENER})

if (build_static)
    add_library(${PROJECT_NAME} STATIC ${TRANSPORT} ${PROTOCOL} ${FACTORY} ${LISTENER})
else()
    add_library(${PROJECT_NAME} SHARED ${TRANSPORT} ${PROTOCOL} ${FACTORY} ${LISTENER})
endif()


if (UNIX)
    if (APPLE)
        target_link_libraries(${PROJECT_NAME} ${Boost_LIBRARIES} pthread)
    else (APPLE)
        target_link_libraries(${PROJECT_NAME} ${Boost_LIBRARIES} pthread rt)
    endif(APPLE)
else()
    target_link_libraries(${PROJECT_NAME} ${Boost_LIBRARIES})
endif(UNIX)

# using compiled library.
target_link_libraries(${PROJECT_NAME}-client ${PROJECT_NAME})

SET(LIBRARY_OUTPUT_PATH lib)
if (NOT build_static)
    install(TARGETS ${PROJECT_NAME} LIBRARY DESTINATION lib)
endif()
install(TARGETS ${PROJECT_NAME}-client RUNTIME DESTINATION .)
